package cn.iocoder.yudao.module.course.content.controller.admin.coursecontent;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.course.content.controller.admin.coursecontent.vo.*;
import cn.iocoder.yudao.module.course.content.dal.dataobject.coursecontent.CourseContentDO;
import cn.iocoder.yudao.module.course.content.convert.coursecontent.CourseContentConvert;
import cn.iocoder.yudao.module.course.content.service.coursecontent.CourseContentService;

@Tag(name = "管理后台 - 课时内容管理")
@RestController
@RequestMapping("/qhsx/course-content")
@Validated
public class CourseContentController {

    @Resource
    private CourseContentService courseContentService;

    @PostMapping("/create")
    @Operation(summary = "创建课时内容管理")
    @PreAuthorize("@ss.hasPermission('qhsx:course-content:create')")
    public CommonResult<Long> createCourseContent(@Valid @RequestBody CourseContentCreateReqVO createReqVO) {
        return success(courseContentService.createCourseContent(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新课时内容管理")
    @PreAuthorize("@ss.hasPermission('qhsx:course-content:update')")
    public CommonResult<Boolean> updateCourseContent(@Valid @RequestBody CourseContentUpdateReqVO updateReqVO) {
        courseContentService.updateCourseContent(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除课时内容管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('qhsx:course-content:delete')")
    public CommonResult<Boolean> deleteCourseContent(@RequestParam("id") Long id) {
        courseContentService.deleteCourseContent(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得课时内容管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('qhsx:course-content:query')")
    public CommonResult<CourseContentRespVO> getCourseContent(@RequestParam("id") Long id) {
        CourseContentDO courseContent = courseContentService.getCourseContent(id);
        return success(CourseContentConvert.INSTANCE.convert(courseContent));
    }

    @GetMapping("/list")
    @Operation(summary = "获得课时内容管理列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('qhsx:course-content:query')")
    public CommonResult<List<CourseContentRespVO>> getCourseContentList(@RequestParam("ids") Collection<Long> ids) {
        List<CourseContentDO> list = courseContentService.getCourseContentList(ids);
        return success(CourseContentConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得课时内容管理分页")
    @PreAuthorize("@ss.hasPermission('qhsx:course-content:query')")
    public CommonResult<PageResult<CourseContentRespVO>> getCourseContentPage(@Valid CourseContentPageReqVO pageVO) {
        PageResult<CourseContentDO> pageResult = courseContentService.getCourseContentPage(pageVO);
        return success(CourseContentConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出课时内容管理 Excel")
    @PreAuthorize("@ss.hasPermission('qhsx:course-content:export')")
    @OperateLog(type = EXPORT)
    public void exportCourseContentExcel(@Valid CourseContentExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<CourseContentDO> list = courseContentService.getCourseContentList(exportReqVO);
        // 导出 Excel
        List<CourseContentExcelVO> datas = CourseContentConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "课时内容管理.xls", "数据", CourseContentExcelVO.class, datas);
    }

}
